//
//  HCommon.m
//  Project
//
//  Created by HuangZhen on 16/8/28.
//  Copyright © 2016年 HuangZhen. All rights reserved.
//

#import "WCommon.h"
#define dispatch_once _dispatch_once
static JGProgressHUD *lastHUD;
@implementation WCommon

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static WCommon *instance;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc]init];
    });
    return instance;
}

+ (void)popupErrorMessage:(NSString *)errorMessage
{
    if (errorMessage.length == 0) {
        [self dismissAnimated:NO];
    }else {
        [self popupErrorMessage:errorMessage after:1];
    }
}

+ (void)popupSuccessMessage:(NSString *)successMessage
{
    [self popupSuccessMessage:successMessage after:1];
}

+ (void)popupErrorMessage:(NSString *)errorMessage after:(NSInteger)after
{
    WProgressHUDErrorIndicatorView *  errorView = [[WProgressHUDErrorIndicatorView alloc]init];
    JGProgressHUD * HUD = [self popWithMessage:errorMessage customView:errorView];
    [HUD dismissAfterDelay:after];
}
+ (void)popupSuccessMessage:(NSString *)successMessage after:(NSInteger)after
{
    WProgressHUDSuccessIndicatorView *  successView = [[WProgressHUDSuccessIndicatorView alloc]init];
    JGProgressHUD * HUD = [self popWithMessage:successMessage customView:successView];
    [HUD dismissAfterDelay:after];
}

+ (JGProgressHUD *)popupMessage:(NSString *)message
{
    return [self popWithMessage:message customView:nil];
}

+ (JGProgressHUD *)popWithMessage:(NSString *)message customView:(JGProgressHUDIndicatorView *)customView
{
    if (lastHUD) {
        [lastHUD dismissAnimated:NO];
    }
    JGProgressHUD * HUD;
    JGProgressHUDStyle style = JGProgressHUDStyleDark;
    if (customView) {
        HUD = [[JGProgressHUD alloc]initWithStyle:style];
        HUD.indicatorView = customView;
    }else {
        HUD =  [JGProgressHUD progressHUDWithStyle:style];
    }
    HUD.textLabel.text = message;
    [HUD showInView:[UIApplication sharedApplication].keyWindow];
    lastHUD = HUD;
    return HUD;
}

+ (void)dismissAnimated:(BOOL)animated {
    [lastHUD dismissAnimated:animated];
    lastHUD = nil;
}

+ (void)dismissMessage
{
    [lastHUD dismiss];
    lastHUD = nil;
}

@end
